<html>
   <head>
      <meta charset = "utf-8">
      <title>Form Prototype</title>
   </head>
   <body>
       <?php 
       
       //Variable Initialization
       
       	   //Each field of the form
           $umid = isset($_POST["umid"]) ? $_POST["umid"]:"";
           $firstName = isset($_POST["firstName"]) ? $_POST["firstName"]:"";
           $lastName = isset($_POST["lastName"]) ? $_POST["lastName"]:"";
           $title = isset($_POST["title"]) ? $_POST["title"]:"";
           $email = isset($_POST["email"]) ? $_POST["email"]:"";
           $phone = isset($_POST["phone"]) ? $_POST["phone"]:"";
           $timeSlot = isset($_POST["timeSlot"]) ? $_POST["timeSlot"]:"";
           
           //A list of the possible errors
           $iserror = false;
           $formerrors = array("umidError"=>false, "firstNameError"=>false, 
           		"lastNameError"=>false, "titleError"=>false, "emailError"=>false,
           		"phoneError"=>false);
           
           //A list of each input
           $inputlist = array("umid"=>"UMID", "firstName"=>"First Name",
           		"lastName"=>"Last Name", "title"=>"Title", "email"=>"Email Address",
           		"phone"=>"Phone Number");
           
           //Error Checking
           
           //This is where you check for errors
           if(isset($_POST["submit"]))
           {
           	   if(!preg_match("/[0-9]{8}/", $umid))
           	   {
           		   $formerrors["umidError"] = true;
           		   $iserror = true;
           	   }
           	   
           	   if(!preg_match("/[A-Za-z]+/", $firstName))
           	   {
           	   	$formerrors["firstNameError"]=true;
           	   	$iserror = true;
           	   }
           	   
           	   if(!preg_match("/[A-Za-z]+/", $lastName))
           	   {
           	   	$formerrors["lastNameError"]=true;
           	   	$iserror = true;
           	   }
           	   
           	   if($title == "")
           	   {
           	   	$formerrors["titleError"]=true;
           	   	$iserror = true;
           	   }

           	   if(!preg_match("/[A-Za-z0-9._%+-]+@[A-Za-z0-9]{1,20}(.[A-Za-z0-9]{1,20}){1,3}/", $email))
           	   {
           	   	$formerrors["emailError"] = true;
           	   	$iserror = true;
           	   }
           	   
           	   if(!preg_match("/[0-9]{3}-[0-9]{3}-[0-9]{4}/", $phone))
           	   {
           	   	$formerrors["phoneError"] = true;
           	   	$iserror = true;
           	   }
           }
           
           //Grab The id of the time that is chosen
           $timeSlotId = substr($timeSlot, 0, 2);
           

           //Open Database
           
           	// Connect to MySQL
           	if ( !( $database = mysql_connect(
           			"richmondj.coscy9cmelyy.us-east-1.rds.amazonaws.com","jason_richmond", "1fltbal2" ) ) )
           		die( "<p>Could not connect to database</p></body></html>" );
           	
           	// open my database
           	if ( !mysql_select_db( "richmondjdb", $database ) )
           		die( "<p>Could not open my database</p>
               </body></html>" ); 

           	//Submit Data
           	if(!$iserror && $umid != false)
           	{
           		// build INSERT query
           		$qinsert= "INSERT INTO students ( umid, firstname, lastname, email, projecttitle, phone, slotid) ";
           				
           		
           		$qvalue = "VALUES ( '$umid', '$firstName', '$lastName', '$email', '$title', '$phone' , '$timeSlot' )";
           		$query = $qinsert.$qvalue;
           		
           		// execute query
           		if ( !( $result = mysql_query( $query, $database ) ) )
           		{
           			print( "<p>Could not execute query!</p>" );
           			die( mysql_error() );
           		} // end if
           		
           		//Update Seats Remaining       		
           		//build update query         
           		$query = "UPDATE slots SET seatsremaining=seatsremaining-1 WHERE idslot='$timeSlotId'";
           		
           		// execute query
           		if ( !( $result = mysql_query( $query, $database ) ) )
           		{
           			print( "<p>Could not execute query!</p>" );
           			die( mysql_error() );
           		} // end if
           	}

           //Page Creation
           
           //Warn the user if there is an error
           if($iserror)
           {
           	   print("<p class = 'error'>Please complete the * fields properly.</p>");
           }
           
           //create the form
           print("<form method = 'post' action = 'index.php'>");
           
           //Add an input for each field
           foreach ( $inputlist as $inputname => $inputalt )
           {
           	print( "<div><label>$inputalt:</label><input type = 'text'
           			name = '$inputname' value = '" . $$inputname . "'>" );
           
           	//if there was an error in the input warn the user
           	if ( $formerrors[ ( $inputname )."Error" ] == true )
           	{           		
           		switch(($inputname)."Error")
           		{
           			case "umidError":
           				print( "<span class = 'error'>* UMID must be 8 digits</span>" );
           				break;
           			case "firstNameError":
           				print( "<span class = 'error'>* Please enter a name.</span>" );
           				break;
           			case "lastNameError":
           				print( "<span class = 'error'>* Please enter a name</span>" );
           				break;
           			case "titleError":
           				print( "<span class = 'error'>* Please enter your project title.</span>" );
           				break;
           			case "emailError":
           				print( "<span class = 'error'>* Wrong email adress format.</span>" );
           				break;
           			case "phoneError":
           				print( "<span class = 'error'>* Wrong phone number format.</span>" );
           				break;
           			default:
           				break;           				
           		}
           	}       		
           
           	print( "</div>" );
           } // end foreach
           
           //Drop Down
           
           /*
            * This will create the dropdown menu for selection
            */
           
           //create dropdown menu
           $query = "SELECT * FROM slots";
           
           // get the slots from database to enter into form
           if ( !( $result = mysql_query( $query, $database ) ) )
           {
           	print( "<p>Could not execute query!</p>" );
           	die( mysql_error() . "</body></html>" );
           } // end if
           
           print("<label>Time Slot:</label>");
           print( "<select name='timeSlot'>" );
           
           // fetch each record in result set
           for ( $counter = 0; $row = mysql_fetch_row( $result ); ++$counter )
           {
           	// build table to display results
           	
           	$text = "";
           	foreach ( $row as $key => $value )
           	{ $text = $text." ".$value; }
           
           	$text = $text." seats available";
           	print( "<option value='$counter'>$text</option>" );
           	$text = "";
           } // end for
           
           print( "</select>" );
           
           print( "<!-- create a submit button -->
            <p class = 'head'><input type = 'submit' name = 'submit'
            value = 'Register'></p></form></body></html>" );
           
           print("<div><a href='classList.php'>Class List</a></div>");
       ?>
   </body>
</html>
